package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtils;
import cc.mrbird.web.domain.MidStudentTemp;
import cc.mrbird.web.domain.RespImport;
import cc.mrbird.web.service.MidStudentService;
import cc.mrbird.web.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;


@Controller
public class MidExcelImportController {

    private static final Logger log = LoggerFactory.getLogger(MidExcelImportController.class);

    @Autowired
    private MidStudentService midStudentService;

    @Log("导入考生信息")
    @RequestMapping("mid_import")
    @RequiresPermissions("mid_import:add")
    public String imports() {
        return "web/mid_import/mid_import";
    }


    @PostMapping("/midStudentImport")
    @ResponseBody
    public RespImport studentImport(@RequestParam("file") MultipartFile file, String userName) {
        log.info("startTime:" + System.currentTimeMillis());
        RespImport respImport = midStudentService.ImportMidStudent(file,userName);
        log.info("endTime:" + System.currentTimeMillis());
        return respImport;
    }

    @RequestMapping("/midDownloadTemplate")
    @ResponseBody
    public ResponseBo midTemplateExcel() {
        MidStudentTemp student1 = new MidStudentTemp();
        student1.setStudentId("201834311111(必须为12位考号)");
        student1.setName("张三（必填）");
        student1.setSesx("女（必填）");
        student1.setProfession("通信工程（必填）");
        student1.setIDnumber("411325000012108008（18位身份证）");
        student1.setReceiver("张三（必填）");
        student1.setDirection("河南省信阳市商城县（必填）");
        student1.setTelephone("15515733013（必填）");
        student1.setScore("90");
        student1.setGraduateAdd("郑州成功财经学院（必填）");
        student1.setNote("备注，允许为空");

        MidStudentTemp student = new MidStudentTemp();
        student.setStudentId("201834311221(必须为12位考号)");
        student.setName("李四（必填）");
        student.setSesx("男（必填）");
        student.setProfession("自动化工程（必填）");
        student.setIDnumber("411325000012108008（18位身份证）");
        student.setReceiver("李四（必填）");
        student.setDirection("河南省信阳市固始县（必填）");
        student.setTelephone("15515733000（必填）");
        student.setScore("200");
        student.setGraduateAdd("郑州成功财经学院管理学（必填）");
        student.setNote("备注，允许为空");

        ArrayList<MidStudentTemp> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        try {
            return FileUtils.createExcelByPOIKit("五年一贯制考生信息模板", list, MidStudentTemp.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
}